using Common.Logging;
using System;
using System.Collections.Generic;
namespace AppToolKit.Model.Validation
{
	public class ValidationManager
	{
		private static readonly ILog logger_ = LogManager.GetLogger(typeof(ValidationManager));
		private Dictionary<string, List<IValidator>> map_;
		private ValidatorQueue updates_;
		private ValidatorQueue saves_;
		private Type entityType_;
		private EntityMeta entityMeta_;
		private ValidatorQueue Updates
		{
			get
			{
				if (this.updates_ == null)
				{
					this.updates_ = new ValidatorQueue();
				}
				return this.updates_;
			}
		}
		private ValidatorQueue Saves
		{
			get
			{
				if (this.saves_ == null)
				{
					this.saves_ = new ValidatorQueue();
				}
				return this.saves_;
			}
		}
		public ValidationManager(Type type, EntityMeta entityMeta)
		{
			this.entityType_ = type;
			this.entityMeta_ = entityMeta;
		}
		public ValidateResultSet Validate(object entity, ValidateBehavior behavior)
		{
			ValidationManager.logger_.Debug(string.Format("进入{0}的ValidateManager进行验证{1}的行为", entity.GetType(), behavior));
			ValidateResultSet validateResultSet = new ValidateResultSet();
			ValidatorQueue validatorQueue = null;
			switch (behavior)
			{
			case ValidateBehavior.Save:
				validatorQueue = this.saves_;
				break;

			case ValidateBehavior.Update:
				validatorQueue = this.updates_;
				break;
			}
			ValidateResultSet result;
			if (validatorQueue == null)
			{
				result = validateResultSet;
			}
			else
			{
				ValidationManager.logger_.Debug(string.Format("找到{0}时的验证器", behavior));
				ValidateContext validateContext = new ValidateContext();
				validateContext.Entity = entity;
				string[] properies = validatorQueue.GetProperies();
				for (int i = 0; i < properies.Length; i++)
				{
					string text = properies[i];
					if (text == ValidateScope.Self)
					{
						validateContext.EntityType = this.entityType_;
						validateContext.NewValue = null;
						validateContext.Property = null;
						validateContext.PropertyDescript = null;
					}
					else
					{
						validateContext.EntityType = this.entityType_;
						validateContext.NewValue = this.GetData(entity, text);
						validateContext.Property = text;
						validateContext.PropertyDescript = this.entityMeta_[text].Descript;
					}
					ValidationManager.logger_.Debug(string.Format("属性{0},新值{1},共{2}个验证器", validateContext.Property, validateContext.NewValue, validatorQueue[text].Count));
					foreach (IValidator current in validatorQueue[text])
					{
						ValidateResult validateResult = null;
						try
						{
							validateResult = current.Validate(validateContext);
						}
						catch (Exception ex)
						{
							ValidationManager.logger_.Error(string.Format("执行验证失败,原因{0}", ex.Message));
						}
						ValidationManager.logger_.Debug(string.Format("使用{0}验证器,结果为{1}", current, (validateResult == null) ? "ok" : validateResult.message));
						if (validateResult != null)
						{
							validateResultSet.AddResult(validateResult);
							break;
						}
					}
				}
				result = validateResultSet;
			}
			return result;
		}
		private object GetData(object entity, string key)
		{
			return entity.GetType().GetProperty(key).GetValue(entity, null);
		}
		public void Merge(ValidatorCollection validators)
		{
		}
		public void Add(string prop, ValidateBehavior behavior, IValidator validator)
		{
			switch (behavior)
			{
			case ValidateBehavior.Save:
				this.Saves.Add(prop, validator);
				break;

			case ValidateBehavior.Update:
				this.Updates.Add(prop, validator);
				break;

			case ValidateBehavior.Both:
				this.Updates.Add(prop, validator);
				this.Saves.Add(prop, validator);
				break;
			}
		}
		public ValidatorCollection GetFieldValidators(string field, ValidateBehavior behavior)
		{
			ValidatorQueue validatorQueue = null;
			switch (behavior)
			{
			case ValidateBehavior.Save:
				validatorQueue = this.saves_;
				break;

			case ValidateBehavior.Update:
				validatorQueue = this.updates_;
				break;
			}
			ValidatorCollection result;
			if (validatorQueue != null)
			{
				result = new ValidatorCollection(this.entityType_, this.entityMeta_, field, validatorQueue[field]);
			}
			else
			{
				result = new ValidatorCollection(this.entityType_, this.entityMeta_, field);
			}
			return result;
		}
	}
}
